package project.parser.ast.types;

import project.environments.StaticEnv;
import project.parser.TypecheckException;
import project.parser.ast.aux.Args;
import project.parser.ast.aux.FuncAbstract;
import project.parser.ast.aux.Param;
import project.parser.ast.aux.Params;

import java.util.List;

public class FuncType extends FuncAbstract implements Type {

	public FuncType(Params params, Type retType) {
		super(params, retType);
	}

	public void argsChek(StaticEnv senv, Args args) { //controllo e typecheck degli argomenti passati
		List argsTypes = args.typecheckList(senv);
		if (argsTypes.size() > params.length())
			throwParamsException("has too many arguments");
		int i = 0;
		for (Param param : params)
			try {
				if (!argsTypes.get(i++).equals(param.getType()))
					throwParamsException("has wrong arguments");
			} catch (IndexOutOfBoundsException e) {
				throwParamsException("does not have enough arguments");
			}
	}

	public Type getReturnType() {
		return retType;
	}

	private void throwParamsException(String message) {
		throw new TypecheckException("A function call " + message);
	}
}
